package com.application.lucky.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.application.lucky.R;
import com.application.lucky.bean.jsonBean.RedPListbean;
import com.application.lucky.request.nav.UrlManager;
import com.application.lucky.utils.CTextUtils;
import com.application.lucky.utils.Flog;
import com.qunar.im.ui.util.MoneyUtils;
import com.application.lucky.widget.TitlebarView;
import com.application.mylibrary.base.BaseActivity;
import com.application.mylibrary.utils.DateUtils;
import com.qunar.im.ui.util.ProfileUtils;
import com.qunar.im.ui.view.recyclerview.BaseQuickAdapter;
import com.qunar.im.ui.view.recyclerview.BaseViewHolder;

import java.util.List;

public class RedPContentActivity extends BaseActivity {
    private static final String TAG = "RedPContentActivity";
    private TitlebarView mTitleBar;
    private RecyclerView mRclView;
    private MYAdapter myAdapter;
    private String id;
    private ImageView mIvRedPHead;
    private TextView mIvRedPName;
    private TextView mIvRedPMoney;
    private TextView mTvRedPNum;
    //    private TextView mTvRedPNum2;
    private RedPListbean data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
//        initView(view);
    }

    @Override
    public void beforeInit() {
        id = getIntent().getStringExtra("id");
        Intent intent = getIntent();
        data = ((RedPListbean) intent.getSerializableExtra("data"));
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_red_pcontent;
    }

    private void initView() {
        mTitleBar = findViewById(R.id.title_bar);
        mTitleBar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finishAnim();
            }

            @Override
            public void rightClick() {
                startActivityAnim(new Intent(RedPContentActivity.this, RedPHistoryActivity.class));
            }
        });
        mRclView = findViewById(R.id.rcl_view);
        mRclView.setLayoutManager(new LinearLayoutManager(this));
        myAdapter = new MYAdapter(R.layout.item_red_p_list);
        mRclView.setAdapter(myAdapter);
        myAdapter.addHeaderView(getHeadView());
        loadData();

    }

    private void loadData() {
        if (data != null) {
            if (data != null) {
                int redNum = data.getRedNum();//红包个数
                String name = data.getUser().getNickName();//名字
                StringBuilder leidian = new StringBuilder();//
                List<Integer> xlist = data.getXlist();
                List<Integer> xmatchList = data.getXmatchList();

                for (Integer integer : xlist) {
                    leidian.append(integer).append("/");
                }
                CTextUtils.Builder builder = CTextUtils.getBuilder("已抢" + redNum + "/" + redNum + "，雷点：");
                for (Integer integer : xlist) {
                    if (xmatchList.contains(integer)) {
                        builder.append(integer + "").setForegroundColor(Color.parseColor("#DE3F35")).append("/");
                    } else {
                        builder.append(integer + "").append("/");
                    }
                }
                mTvRedPNum.setText(builder.create());
//                mTvRedPNum2.setText(leidian.toString());
                CTextUtils.getBuilder("¥")
                        .append(MoneyUtils.getMoney(data.getMoney())).setProportion(2).into(mIvRedPMoney);
//                mIvRedPMoney.setText( ;);
                mIvRedPName.setText(data.getUser().getNickName() + "发出的红包");
                ProfileUtils.displayGravatarRoundedCornerByImageSrc(this, UrlManager.getHeadPic(data.getUser().getHeadPic()), mIvRedPHead);
//                String s = dataInfo.getxMatchNo();
//                List<Integer> xlist = dataInfo.getXlist();
//                String odds = dataInfo.getOdds();
//                String redSum = dataInfo.getRedSum();
                myAdapter.setTime(data.getCreateTime());
                List<RedPListbean.XuserInfoBeanListBean> beanList = data.getXuserInfoBeanList();
                if (beanList != null) {
                    myAdapter.setNewData(beanList);
                }
            }
        }
    }

    private View getHeadView() {
        View view = LayoutInflater.from(this).inflate(R.layout.view_redp_head_view, null);
        mIvRedPHead = (ImageView) view.findViewById(R.id.iv_red_p_head);
        mIvRedPName = (TextView) view.findViewById(R.id.iv_red_p_name);
        mIvRedPMoney = (TextView) view.findViewById(R.id.iv_red_p_money);
        mTvRedPNum = (TextView) view.findViewById(R.id.tv_red_p_num);
//        mTvRedPNum2 = (TextView) view.findViewById(R.id.tv_red_p_num2);
        return view;
    }

    static class MYAdapter extends BaseQuickAdapter<RedPListbean.XuserInfoBeanListBean, BaseViewHolder> {
        private String showOnceTime = "";

        public MYAdapter(int layoutResId) {
            super(layoutResId);

        }

        private long time;

        public long getTime() {
            return time;
        }

        public void setTime(long time) {
            this.time = time;
        }

        @Override
        protected void convert(BaseViewHolder helper, RedPListbean.XuserInfoBeanListBean item) {
            ImageView view = helper.getView(R.id.iv_red_p_list_head);
            int size = getData().size();
            int i = 3000 / size;

            ProfileUtils.displayGravatarRoundedCornerByImageSrc(((Activity) mContext), UrlManager.getHeadPic(item.getUser().getHeadPic()), view);

            if (TextUtils.isEmpty(showOnceTime)) {
                showOnceTime = DateUtils.getRedTime(time + (3000 - helper.getAdapterPosition() * i));
            }
            helper.setText(R.id.iv_red_p_list_time, showOnceTime)
                    .setText(R.id.iv_red_p_list_name, getFakeName(helper.getAdapterPosition()))
                    .setText(R.id.iv_red_p_list_money, "¥" + MoneyUtils.getMoney(item.getMoney()))
                    .setVisible(R.id.iv_red_p_list_isLei, item.isMatch());
        }

        private String getFakeName(int pos) {
            return "欢乐禁抢" + pos;
        }
    }
}
